package com.kaibes.orm.core.util;

import java.util.Arrays;

import com.kaibes.core.base.util.MathUtils;
import com.kaibes.orm.core.core.Page;

public class PageUtils {

    /**
     * -99是空 -9是省略
     * 
     * @param page
     * @return
     */
    public static long[] getPages(Page<?> page) {
        if (page.getPageIndex() > page.getTotalPage()) {
            return null;
        }

        long[] ls = new long[7];
        ls[0] = 1;
        if (page.getPageIndex() < 5) {
            ls[1] = 2;
        } else {
            ls[1] = 0;
        }
        
        if (MathUtils.in(page.getPageIndex(), 1, 2, 3)) {
            ls[2] = 3;
            ls[3] = 4;
            ls[4] = 5;
        } else if (MathUtils.in(page.getPageIndex(), page.getTotalPage(), page.getTotalPage()-1, page.getTotalPage()-2)) {
            ls[2] = page.getTotalPage() - 4;
            ls[3] = page.getTotalPage() - 3;
            ls[4] = page.getTotalPage() - 2;
        } else {
            ls[2] = page.getPageIndex() - 1;
            ls[3] = page.getPageIndex();
            ls[4] = page.getPageIndex() + 1;
        }
        if (page.getPageIndex() > page.getTotalPage() - 4) {
            ls[5] = page.getTotalPage() - 1;
        } else {
            ls[5] = 0;
        }
        ls[6] = page.getTotalPage();

        if (page.getTotalPage() < 2) {
            ls[1] = -99;
        }
        if (page.getTotalPage() < 3) {
            ls[2] = -99;
        }
        if (page.getTotalPage() < 4) {
            ls[3] = -99;
        }
        if (page.getTotalPage() < 5) {
            ls[4] = -99;
        }
        if (page.getTotalPage() < 6) {
            ls[5] = -99;
        }
        if (page.getTotalPage() < 7) {
            ls[6] = -99;
        }

        for (int i = 0; i < ls.length; i++) {
            if (MathUtils.notBetween(ls[i], page.getTotalPage(), 0)) {
                ls[i] = -99;
            } else {
                if (ls[i] < 1) {
                    continue;
                }
                for (int j = 0; j < i; j++) {
                    if (ls[i] == ls[j]) {
                        ls[i] = -99;
                    }
                }
            }
        }

        return ls;
    }

    public static <E> Page<E> createOnePage(E data) {
        Page<E> page = new Page<E>(1, 1);
        page.setData(Arrays.asList(data));
        page.setTotalCount(1);
        page.setTotalPage(1);
        return page;
    }

}
